package top.easycode.app.service.impl;

import top.easycode.app.common.BaseRsp;
import top.easycode.app.constants.RespCode;
import top.easycode.app.controller.service.dto.rsp.UserInfoRsp;
import top.easycode.app.dao.UserInfoMapper;
import top.easycode.app.model.UserInfo;
import top.easycode.app.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.easycode.app.util.BeanUtil;

import javax.validation.constraints.NotNull;

/**
 * Created by HUJUN on 2016/8/29.
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Override
    public UserInfoRsp getUserInfoByUserId(Integer userId) {
        UserInfoRsp userInfoRsp= new UserInfoRsp();
        UserInfo user = userInfoMapper.selectByPrimaryKey(userId);
        if(user == null){
            userInfoRsp.setRespCode(RespCode.E_1024.getCode());
            userInfoRsp.setRespDesc(RespCode.E_1024.getName());
        }



        return userInfoRsp;
    }

    @Override
    public UserInfoRsp userLogin(@NotNull(message = "登录账号不能为空")String username, @NotNull(message = "登录密码不能为空")String password) {
        UserInfoRsp userInfoRsp= new UserInfoRsp();
        UserInfo user = userInfoMapper.selectByName(username);
         if(user == null){
             userInfoRsp.setRespCode(RespCode.E_1017.getCode());
             userInfoRsp.setRespDesc(RespCode.E_1017.getName());
         }else{
             if(user.getPassword().equals(password)) {
                 BeanUtil.beanCopyByEqualName(user, userInfoRsp);
                 userInfoRsp.setRespCode(RespCode.E_0000.getCode());
                 userInfoRsp.setRespDesc(RespCode.E_0000.getName());
             }else{
                 userInfoRsp.setRespCode(RespCode.E_1018.getCode());
                 userInfoRsp.setRespDesc(RespCode.E_1018.getName());
             }
         }
         return userInfoRsp;
    }

    @Override
    public UserInfoRsp userRegister(@NotNull(message = "登录账号不能为空")String name,@NotNull(message = "登录密码不能为空")String password,String confirmPassword,String nickname,String mobile,String headImage) {
        UserInfoRsp rsp = new UserInfoRsp();
        if(!password.equals(confirmPassword)){
            rsp.setRespCode(RespCode.E_1026.getCode());
            rsp.setRespDesc(RespCode.E_1026.getName());
            return rsp;
        }
        UserInfo user = new UserInfo();
        user.setName(name);
        user.setMobile(mobile);
        user.setHeadImage(headImage);
        user.setPassword(password);
        user.setNickname(nickname);

        int num =userInfoMapper.insertSelective(user);

        if(num > 0){
            rsp.setRespCode(RespCode.E_0000.getCode());
            rsp.setRespDesc(RespCode.E_0000.getName());
            BeanUtil.beanCopyByEqualName(user,rsp);
        }else{
            rsp.setRespCode(RespCode.E_1025.getCode());
            rsp.setRespDesc(RespCode.E_1025.getName());
        }
        //token暂无
//        user.setToken();
        return rsp;
    }
    private BaseRsp createSuccess(){
        BaseRsp rsp = new BaseRsp();
        rsp.setRespCode(RespCode.E_0000.getCode());
        rsp.setRespDesc(RespCode.E_0000.getName());
        return rsp;
    }
    private BaseRsp createRsp(RespCode respCode){
        BaseRsp rsp = new BaseRsp();
        rsp.setRespCode(respCode.getCode());
        rsp.setRespDesc(respCode.getName());
        return rsp;
    }
}
